﻿using Logistics.Manager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Logistics
{
    public partial class frmSAPExport
    {
        //老物流
        private void old_export(string tc_owr009, string tc_owr002, string tc_owr006, string tc_owr007)
        {
            _directoryPath = Path.Combine(Application.StartupPath, @"LG\" + tc_owr009);
            if (!Directory.Exists(_directoryPath))
                Directory.CreateDirectory(_directoryPath);
            Logger.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + tc_owr009;
            try
            {
                _old_exportData.old_PrepareData(tc_owr009);
                var fileName = old_BuildOwrData(tc_owr009, tc_owr002, tc_owr006,tc_owr007);
                old_BuildOwwData(tc_owr009, fileName);
                old_BuildOccData(tc_owr009, fileName);

                //  _old_exportData.new_EndUpdData(tc_owr009, fileName);


            }
            catch (Exception ex)
            {
                var files = Directory.GetFiles(_directoryPath);
                foreach (var item in files)
                {
                    try
                    {
                        File.Delete(item);
                    }
                    catch { }
                }
                Logger.WriteLine("生成txt文件出错；详细信息：" + ex.ToString());
                MessageBox.Show("生成txt文件出错，请重试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            UpLoadFile("tiptop", "toperp2011", "ftp://192.168.80.16/", $"/u1/topprod/topcust/edi_test/LG/{tc_owr009}/SEND/", "Y");
            MessageBox.Show("数据导出成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // System.Diagnostics.Process.Start("notepad.exe", Logger.FilePath);
        }
        /// <summary>
        /// 1.老物流_单据传输资料主档
        /// </summary>
        private string old_BuildOwrData(string tc_owr009, string tc_owr002, string tc_owr006, string tc_owr007)
        {
            //OWR
            var result = _old_exportData.old_PrepareOwrData(tc_owr009, tc_owr002, tc_owr006,tc_owr007);
            var local_fileName = $"LINFOMP{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);

            var fileName = "/u1/topprod/topcust/edi_test/LG/" + tc_owr009.ToString() + "/SEND/" + local_fileName;

            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.tc_owr001}{item.tc_owr002} {item.tc_ows002}1{item.tc_owr005} {item.tc_owr006} {item.tc_owr007 }{item.tc_owr009} {item.tc_ows003} {item.l_temp_add}{item.tc_ows004}{item.tc_ows005} {item.tc_ows006} {item.tc_ows007.ToString("0.00")} {item.tc_ows008.ToString("0.00")} {item.tc_ows010.ToString("0.00")} {item.tc_owr0131}/{item.tc_owr013} {item.tc_owr014} {item.tc_ows011.ToString("0.00")} {item.tc_ows011_010.ToString("0.00")}";
                sw.WriteLine(str);
                var owfModel = new Model.TC_OWF_FILE();
                owfModel.TC_OWF001 = fileName;
                owfModel.TC_OWF003 = item.tc_owr002;
                owfModel.TC_OWF004 = item.tc_owr001;
                owfModel.TC_OWF005 = DateTime.Now.ToString("HH:mm:ss");
                owfModel.TC_OWFDATE = DateTime.Now.ToString("yyyy-MM-dd");
                owfModel.TC_OWFUSER = MyForms.User.UserName;
                _old_exportData.old_SaveOwrData(owfModel);
            }
            sw.Flush();
            sw.Close();

            //OWR_O备注
            local_fileName = $"LORDMKP{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            local_filePath = Path.Combine(_directoryPath, local_fileName);
            fs = new FileStream(local_filePath, FileMode.Create);
            sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = "";
                if (!string.IsNullOrEmpty(item.tc_owr016))
                {
                    str = $"{item.tc_owr002} {item.tc_owr016} {item.tc_owr009} 0.0000        0.00          0.00 ";
                    sw.WriteLine(str);
                }

                if (!string.IsNullOrEmpty(item.tc_owr017))
                {
                    str = $"{item.tc_owr002} {item.tc_owr017} {item.tc_owr009} 0.0000        0.00          0.00 ";
                    sw.WriteLine(str);
                }
                if (!string.IsNullOrEmpty(item.tc_owr018))
                {
                    str = $"{item.tc_owr002} {item.tc_owr018} {item.tc_owr009} 0.0000        0.00          0.00 ";
                    sw.WriteLine(str);
                }


            }
            sw.Flush();
            sw.Close();

            return fileName;
        }


        /// <summary>
        /// 2.单据传输资料折扣档
        /// </summary>
        private void old_BuildOwwData(string tc_owr009, string tc_owf001)
        {
            var result = _old_exportData.old_PrepareOwwData(tc_owf001);

            var local_fileName = $"LGINFZK{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.TC_OWW001} {item.TC_OWW002} {item.TC_OWW003} {item.TC_OWW004 }";
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }


        /// <summary>
        /// 3.客户基本资料档
        /// </summary>
        private void old_BuildOccData(string tc_owr009, string tc_owf001)
        {
            var result = _old_exportData.old_PrepareOccData(tc_owf001);
            var local_fileName = $"LCUSTMP{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.occ01} {item.occ18} {item.occ242} {item.occ294 } {item.occ304} {item.occ29} {item.occ30} {item.tc_owr009} {item.ta_occ001} {item.occ041}";
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
            fs.Close();

            local_fileName = $"LCSTSHP{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            local_filePath = Path.Combine(_directoryPath, local_fileName);
            fs = new FileStream(local_filePath, FileMode.Create);
            sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.tc_owr005} {item.l_temp_add} {item.occ242} 3 {item.occ294 } {item.occ304 } {item.occ296} {item.occ29} {item.occ30} {item.occ30} {item.tc_owr009} {item.occdate}";
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }


        /// <summary>
        /// 4.客户货号关系资料档 
        /// </summary>
        private void old_Buildtc_obkData(string tc_owr009, string tc_owf001)
        {
            var result = _old_exportData.old_Preparetc_obkData(tc_owf001);
            var local_fileName = $"LCSTITM{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.ima01} {item.occ01} {item.tc_obk03} {item.tc_obk05 } {item.tc_obk07} ";
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// 5.商品基本资料文件名
        /// </summary>
        private void old_BuildImaData(string tc_owr009, string tc_owf001)
        {
            var result = _old_exportData.old_PrepareImaData(tc_owf001);
            var local_fileName = $"LITMASP{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("ASCII"));
            foreach (var item in result)
            {
                var str = $"{item.ima01} {item.ta_ima001} {item.ima02} {item.ima31_fac } {item.ima06} {item.ima31} {item.ima31_dec} {item.ima25} {item.ima25_dec} {item.ima135} {item.ta_ima027} {item.ta_ima028} ";
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }

    }
}
